﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClientRegister
{
    public partial class AddEmployeeForm : Form
    {

        SynchronizeData sync = new SynchronizeData();

        GetNextID getId = new GetNextID();

        public AddEmployeeForm()
        {
            InitializeComponent();
        }

        DataSet dsgender, dsrole;
        string IdGender, IdRole;

        private void AddEmployeeForm_Load(object sender, EventArgs e)
        {
            ////Load du lieu tu server
            //sync.GetDataForInsertMethodWithID(sync.EmployTb, sync.insertcol);
            //sync.GetDataForUpdateMethodWithID(sync.EmployTb, sync.updatecol);

            //load combox gioi tinh
            GenderDataProvider gender = new GenderDataProvider();
            dsgender = gender.loadDs();
            cbbGender.DataSource = dsgender.Tables[0];
            cbbGender.DisplayMember = "Gender";
            cbbGender.ValueMember = "IDGender";

            //load combox role
            RoleDataProvider role = new RoleDataProvider();
            dsrole = role.loadRole();
            cbbRole.DataSource = dsrole.Tables[0];
            cbbRole.DisplayMember = "Role";
            cbbRole.ValueMember = "IDRole";
        }

        private void cbbGender_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbGender.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                IdGender = cbbGender.SelectedValue.ToString();
            }
        }

        private void cbbRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbRole.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                IdRole = cbbRole.SelectedValue.ToString();
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            EmployeesDataProvider addemp = new EmployeesDataProvider();
            if (txtpass.Text == "")
                MessageBox.Show("Ô mật khẩu không được trống");
            else
            {
                string lastId = getId.getLastID(getId.EmpTB, getId.IDCol);
                string ID = getId.NextID(lastId, getId.prefixID);

                int result = addemp.AddEmployee(ID,txtpass.Text, txtName.Text, txtBirth.Text, IdGender, txtAddr.Text, txtPhone.Text, IdRole);
                if (result != 0)
                {
                    //string xml = addemp.SelectAndConvertXMLEmp(idEmp);
                    ////Chuyen thong tin len server
                    //sync.transfDataForInsertMethod(xml, sync.EmployTb);
                    MessageBox.Show("Đã thêm nhân viên sử dụng phần mềm");
                }
                else
                {
                    MessageBox.Show("Không thêm được");
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
